package com.hwadee.hello.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hwadee.hello.dao.StudentDao2;
import com.hwadee.hello.entity.Student;

public class UpdateServlet extends HttpServlet {

	private StudentDao2 dao = new StudentDao2();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String id = req.getParameter("id");
		req.setAttribute("id", id);
		Student stu = dao.findById(Integer.parseInt(id));
		req.setAttribute("stu", stu);
		req.getRequestDispatcher("/WEB-INF/views/student/update.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		
		String stuId = req.getParameter("stuId");
		Student stu = dao.findById(Integer.parseInt(stuId));
		resp.setContentType("text/html;charset=UTF-8");
		
		String stuName = req.getParameter("stuName");
		String stuSex = req.getParameter("stuSex");
		String stuAge = req.getParameter("stuAge");
		String stuPro = req.getParameter("stuPro");
		stu.setStuName(stuName);
		stu.setStuSex(stuSex);
		stu.setStuAge(Integer.parseInt(stuAge));
		stu.setStuPro(stuPro);
		
		
		
		int rows = dao.update(stu);
		resp.sendRedirect("/hello/students");
		
		
	}

	
}
